package cn.sxjzit.officlal.common;

import lombok.Data;
import lombok.Getter;

@Data
public class Result<T> {
    // 状态码
    private Integer code;
    //返回的消息
    private String message;
    //返回的数据
    private T data;
    public static <T> Result<T> build(T data){
        Result<T> result = new Result<>();
        if (data != null){
            result.setData(data);
        }
        return result;
    }
    public static <T> Result<T> build(Integer code,String message, T data){
        Result<T> result = build(data);
        result.setCode(code);
        result.setMessage(message);
        return result;
    }
    public static <T> Result<T> build( T data,ResutlCodeEnum resutlCodeEnum){
        Result<T> result = build(data);
        result.setCode(resutlCodeEnum.getCode());
        result.setMessage(resutlCodeEnum.getMessage());
        return result;
    }
    //成功 没有数据
    public static <T> Result<T> ok(){
        return Result.ok(null);
    }
    public static <T> Result<T> ok(T data){
        Result<T> result = build(data);
        return build(data,ResutlCodeEnum.SUCCESS);
    }
    public static <T> Result<T> fail(T data){
        Result<T> result = build(data);
        return build(data,ResutlCodeEnum.FAIL);
    }
    public static <T> Result<T> fail(){
        return Result.fail(null);
    }
    public Result<T> messsage(String msg){
        this.setMessage(msg);
        return this;
    }
    public Result<T> code(Integer code){
        this.setCode(code);
        return this;
    }
}
